/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.report;

/**
 *
 * @author Yusup Jauhari Shandi <in.visi>
 */

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.j2ee.servlets.BaseHttpServlet;
import src.DBManager;


import src.util.ReportConfigUtil;

public abstract class AbstractBaseReportBean {
	public enum ExportOption {PDF, HTML, EXCEL, RTF}
	
	private ExportOption exportOption;
	
	private final String COMPILE_DIR = "/reports/";
        private HashMap<String,Object> reportParameters;
			
	public AbstractBaseReportBean() {
		super();
                reportParameters = new HashMap<>();
		setExportOption(ExportOption.PDF);
	}

	protected void prepareReport() throws JRException, IOException {
		ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
		
		ServletContext context = (ServletContext) externalContext.getContext();
		HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
		HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
		
		ReportConfigUtil.compileReport(context, getCompileDir(), getCompileFileName());
		
		File reportFile = new File(ReportConfigUtil.getJasperFilePath(context, getCompileDir(), getCompileFileName()+".jasper"));
		//JRDataSource jr = getJRDataSource();
		JasperPrint jasperPrint = ReportConfigUtil.fillReport(reportFile, reportParameters, getJRDataSource());
		
		if(getExportOption().equals(ExportOption.HTML)) {
			ReportConfigUtil.exportReportAsHtml(jasperPrint, response.getWriter());
		}else {		
			//chan request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);		
			//chan response.sendRedirect(request.getContextPath()+"/faces/reports/"+getExportOption());
                        response.setContentType("application/pdf");            
                        response.addHeader("Content-disposition", "attachment; filename=\""+jasperPrint.getName()+".pdf\"");
                        JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
		}
		
		FacesContext.getCurrentInstance().responseComplete();
	}

        //via or using connection
    protected void prepareReportViaConn() throws JRException, IOException {
		ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
		
		ServletContext context = (ServletContext) externalContext.getContext();
		HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
		HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
		
		ReportConfigUtil.compileReport(context, getCompileDir(), getCompileFileName());
		
		File reportFile = new File(ReportConfigUtil.getJasperFilePath(context, getCompileDir(), getCompileFileName()+".jasper"));
		//JRDataSource jr = getJRDataSource();
              // JasperReport jasperReport = (JasperReport)JRLoader.loadObjectFromFile(reportFile.getPath() ); 
               //String sql = jasperReport.getQuery().getText();
		JasperPrint jasperPrint = ReportConfigUtil.fillReport(reportFile, reportParameters);
		
		if(getExportOption().equals(ExportOption.HTML)) {
			ReportConfigUtil.exportReportAsHtml(jasperPrint, response.getWriter());
		}else {		
			//chan request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);		
			//chan response.sendRedirect(request.getContextPath()+"/faces/reports/"+getExportOption());
                        response.setContentType("application/pdf");            
                        response.addHeader("Content-disposition", "attachment; filename=\""+jasperPrint.getName()+".pdf\"");
                        JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
		}
		
		FacesContext.getCurrentInstance().responseComplete();
                
    }
    
        //via or using connection & kirim datasource
    protected void prepareReportViaConn(JRDataSource datasource) throws JRException, IOException {
		ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
		
		ServletContext context = (ServletContext) externalContext.getContext();
		HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
		HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
		
		ReportConfigUtil.compileReport(context, getCompileDir(), getCompileFileName());
		
		File reportFile = new File(ReportConfigUtil.getJasperFilePath(context, getCompileDir(), getCompileFileName()+".jasper"));
		//JRDataSource jr = getJRDataSource();
                JasperPrint jasperPrint = null;
                if (datasource!=null)
                    jasperPrint = ReportConfigUtil.fillReport(reportFile, reportParameters,datasource);
                else
                    jasperPrint = ReportConfigUtil.fillReport(reportFile, reportParameters);
		
		if(getExportOption().equals(ExportOption.HTML)) {
			ReportConfigUtil.exportReportAsHtml(jasperPrint, response.getWriter());
		}else {		
			//chan request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);		
			//chan response.sendRedirect(request.getContextPath()+"/faces/reports/"+getExportOption());
                        response.setContentType("application/pdf");            
                        response.addHeader("Content-disposition", "attachment; filename=\""+jasperPrint.getName()+".pdf\"");
                        JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
		}
		
		FacesContext.getCurrentInstance().responseComplete();
    }
    
    public HashMap<String, Object> getReportParameters() {
        return reportParameters;
    }

    public void setReportParameters(HashMap<String, Object> reportParameters) {
        this.reportParameters = reportParameters;
    }

        
	public ExportOption getExportOption() {
		return exportOption;
	}

	public void setExportOption(ExportOption exportOption) {
		this.exportOption = exportOption;
	}

	/*protected Map<String, Object> getReportParameters() {
		return new HashMap<String, Object>();
	}
        * 
        */

	protected String getCompileDir() {
		return COMPILE_DIR;
	}
	
	protected abstract JRDataSource getJRDataSource();

	protected abstract String getCompileFileName();

}
